<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods.categories">
      <!-- 面包屑 -->
      <!-- 发送请求：需不要时间 => 需要  =>  请求回来之前 goods {} -->
      <BreadCrumbs>
        <XtxBread to="/">首页</XtxBread>
        <XtxBread :to="'/category/' + goods.categories[1].id">{{
          goods.categories[1].name
        }}</XtxBread>
        <XtxBread :to="'/category/sub/' + goods.categories[0].id">{{
          goods.categories[0].name
        }}</XtxBread>
        <XtxBread>{{ goods.name }}</XtxBread>
      </BreadCrumbs>
      <div v-if="Object.keys(goods).length" class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <!-- 图片预览区 -->
          <XtxImageView :image-list="goods.mainPictures" />
          <GoodsSale :goods="goods" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsInfo :goods="goods" />
          <GoodsSku :goods="goods" @getObjId="getObjId" />
          <Numbox :max="goods.inventory" v-model="count"/>
          <Button class="btn" type="primary" @click="addGoods">加入购物车</Button>
        </div>
      </div>
       <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品详情 -->
          <GoodsDetail :goods="goods"/>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqFindGoods } from '@/api/goods'
import { reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'

import GoodsInfo from './components/goods-info.vue'
import GoodsSale from './components/goods-sale.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { message } from '@/components/Message'

export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsInfo,
    GoodsSale,
    GoodsSku,
    GoodsDetail,
    GoodsHot
  },
  setup () {
    const store = useStore()
    const count = ref(1)
    const route = useRoute()
    const goods = ref({})
    let objGood = reactive({})
    async function getGoods () {
      const res = await reqFindGoods(route.params.id)
      goods.value = res.result
    }
    // 子组件将数据返回
    const getObjId = (obj) => {
      objGood = Object.assign(objGood, obj)
    }
    // 加入购物车
    const addGoods = () => {
      if (!Object.keys(objGood).length) return message({ type: 'error', text: '请选择规格！' })
      if (!objGood.inventory) return message({ type: 'error', text: '库存不足！' })
      store.dispatch('cart/setList', {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: objGood.id,
        price: objGood.oldPrice,
        nowPrice: objGood.price,
        attrsText: objGood.specsText,
        stock: objGood.inventory,
        selected: true, // 选中
        isEffective: true, // 是否有效
        count: count.value // 商品数量
      })
      // console.log(objGood, 'gggg')
      // console.log(goods, 'gggglll')
    }
    getGoods()
    return {
      goods,
      getObjId,
      count,
      addGoods
    }
  }
}
</script>

<style lang="less" scoped>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.btn {
  margin: 10px 0;
}
</style>
